﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ESMAM.DAL.SYS;
using ESMAM.Model;

namespace ESMAM.BLL.SYS
{
    public class LangBLL
    {
        private static LangDAL dallang = new LangDAL();

        public static NameValueCollection dict_CN_EN = null;
        public static NameValueCollection dict_CN_JP = null;
        public static NameValueCollection dict_CN_KOR = null;
        public static NameValueCollection dict_CN_SPA = null;

        static LangBLL()
        {
            Refresh();
        }


        public static void Refresh()
        {
            var data = dallang.GetAll();
            dict_CN_EN = data.MapperNameValues("zh", "en");
            //dict_CN_JP = data.MapperNameValues("zh", "jp");
            //dict_CN_KOR = data.MapperNameValues("zh", "kor");
            dict_CN_SPA = data.MapperNameValues("zh", "spa");
        }


        public static Retful GetPager(NameValueCollection nvc)
        {
            Pager pager = null;
            try
            {
                pager = dallang.GetPager(new ReqParam(nvc));
            }
            catch (Exception ex)
            {
                return Retful.CreateFail(ex.Message);
            }
            return Retful.CreateSuccess(pager);
        }

        public static Retful Delete(string id)
        {
            bool isOk = false;
            try
            {
                isOk = dallang.Delete(id);
                if (!isOk)
                {
                    return Retful.CreateFail("未知原因，更新失败！");
                }
            }
            catch (Exception ex)
            {
                return Retful.CreateFail(ex.Message);
            }
            return Retful.CreateSuccess(null, "删除成功！");
        }

        public static Retful Upsert(NameValueCollection nvc)
        {
            try
            {
                var kv = dallang.Upsert(nvc);

                if (kv.Key)
                {
                    Refresh();
                }
                return Retful.CreateRetful(kv.Key ? 1 : -1, kv.Value);
            }
            catch (Exception ex)
            {
                return Retful.CreateFail(ex.Message);
            }
        }

        public static Retful SetLang(string uid, Lang lg)
        {
            bool isOk = false;
            try
            {
                isOk = dallang.SetLang(uid,lg);
                if (!isOk)
                {
                    return Retful.CreateFail("未知原因，更新失败！");
                }
            }
            catch (Exception ex)
            {
                return Retful.CreateFail(ex.Message);
            }
            return Retful.CreateSuccess(null, "更新成功！");
        }

        public static Retful FanYin(string type, bool isReplace)
        {
            bool isOk = false;
            try
            {
                isOk = dallang.FanYin(type, isReplace);
                if (!isOk)
                {
                    return Retful.CreateFail("未知原因，更新失败！");
                }

                if (type.Equals("lang",StringComparison.OrdinalIgnoreCase))
                {
                    Refresh();
                }
                else
                {
                    MenuBLL.RefreshAllMenu();
                }
            }
            catch (Exception ex)
            {
                return Retful.CreateFail(ex.Message);
            }
            return Retful.CreateSuccess(null, "更新成功！");
        }
    }
}
